package controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import dao.UserDAO;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/updateUser")
public class UpdateUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {

        // 读取JSON请求体
        BufferedReader reader = request.getReader();
        Gson gson = new Gson();
        JsonObject json = gson.fromJson(reader, JsonObject.class);

        int id = json.get("id").getAsInt();
        String username = json.get("username").getAsString();
        String email = json.get("email").getAsString();

        UserDAO userDAO = new UserDAO();
        boolean success = userDAO.updateUser(id, username, email);

        response.setContentType("application/json");
        response.getWriter().print("{\"success\": " + success + "}");
    }
}